package com.zbf.auth.login;

import com.alibaba.fastjson.JSONObject;
import com.zbf.auth.dao.UserDao;
import com.zbf.common.entity.ResponseResult;
import com.zbf.common.entity.ResponseResultEnum;
import com.zbf.common.utils.FreemarkerUtils;
import com.zbf.common.utils.JWTUtis;
import io.jsonwebtoken.ExpiredJwtException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
import java.util.HashMap;

/**
 * 作者: LCG
 * 日期: 2020/6/15 19:03
 * 描述:
 */

@Controller
public class ActiveController {

    @Autowired
    private UserDao userDao;

    /**
     * 激活新账户
     * @param code 激活码
     * @return
     */
    @RequestMapping("/toActive")
    public void toActive(String code, HttpServletResponse httpServletResponse){
        ResponseResult responseResult = ResponseResult.getResponseResult();
        try{
            //获取参数解密信息
            JSONObject jsonObject = JWTUtis.decodeJwtTockenForActive(code);
            //获取用户ID
            Long userId=(Long)jsonObject.get("userId");
            //更新用户信息
            userDao.toUpdateUserActive(userId);
            //返回操作状态
            responseResult.setSuccess(ResponseResultEnum.SECCESS.getMessage());
            responseResult.setCode(ResponseResultEnum.SECCESS.getCode());
            httpServletResponse.setContentType("text/html;charset=UTF-8");
            PrintWriter writer = httpServletResponse.getWriter();

            HashMap<String, Object> stringObjectHashMap = new HashMap<>();
            stringObjectHashMap.put("loginPath","http://localhost:8080/");

            FreemarkerUtils.getStaticHtml(ActiveController.class,
                    "/activePage/",
                    "activeOk.html",stringObjectHashMap,writer);

        }catch (Exception e){
            e.printStackTrace();

            //失败信息
            responseResult.setSuccess(ResponseResultEnum.FAILURE.getMessage());
            responseResult.setCode(ResponseResultEnum.FAILURE.getCode());


        }


    }

}
